package com.jcgyl;



import java.util.Collections;

import javax.servlet.ServletException;
import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;

import org.apache.catalina.servlet4preview.ServletContext;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import com.jcgyl.export.util.UploadReceiver;
import com.jcgyl.export.websocket.ProcessWebSocketHandler;

@SpringBootApplication
@MapperScan({ "com.jcgyl.export.dao", "com.jcgyl.export.mapper" })
@Configuration
@EnableWebSocket
@EnableAutoConfiguration

public class OMS extends SpringBootServletInitializer implements WebSocketConfigurer {

	/**
	 * 使用代码注册Servlet（不需要@ServletComponentScan注解）
	 */
	
	@Bean
	public ServletRegistrationBean servletRegistrationBean() {
		return new ServletRegistrationBean(new UploadReceiver(), "/FileUpload/*");// ServletName默认值为首字母小写，即myServlet
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		builder.sources(this.getClass());
		return super.configure(builder);
	}

	public static void main(String[] args) throws Exception {
		SpringApplication.run(OMS.class, args);
	}

	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(processWebSocketHandler(), "/process").withSockJS();
	}

	@Bean
	public WebSocketHandler processWebSocketHandler() {
		return new ProcessWebSocketHandler();
	}
	/*
	 * (non-Javadoc)
	 * @see org.springframework.boot.web.support.SpringBootServletInitializer#onStartup(javax.servlet.ServletContext)
	 * 解决URL中出现的jsessionid的问题。
	 * JL
	 */
	/*public void onStartup(ServletContext servletContext) throws ServletException{
		super.onStartup(servletContext);
		servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
		SessionCookieConfig sessionCookieConfig = servletContext.getSessionCookieConfig();
		sessionCookieConfig.setHttpOnly(true);
	}*/
}